<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ include file="/common/taglibs.jsp"%>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/html">
<head>
   	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta http-equiv="X-UA-Compatible" content="chrome=1">
    <link href="<%=request.getContextPath()%>/css/bootstrap.css" rel="stylesheet" type="text/css">
    <script src="<%=request.getContextPath()%>/js/jquery-2.0.3.min.js" type="text/javascript"></script>
    <script src="<%=request.getContextPath()%>/js/bootstrap.js" type="text/javascript"></script>  
    
    <link rel="stylesheet" href="<%=request.getContextPath()%>/css/bootstrapValidator.css"/>
    <script type="text/javascript" src="<%=request.getContextPath()%>/js/bootstrapValidator.js"></script>
    <script src="<%=request.getContextPath()%>/js/jquery.twbsPagination.js" type="text/javascript"></script>  
    
    <script type="text/javascript" src="<%=request.getContextPath()%>/js/lhdialog/lhgcore.min.js"></script>
    <script type="text/javascript" src="<%=request.getContextPath()%>/js/lhdialog/lhgdialog.js"></script>
    <script type="text/javascript" src="<%=request.getContextPath()%>/js/OpenDialog.js"></script> 
    <script type="text/javascript">
    
    $(document).ready(function () {
	    $('#pagination-demo').twbsPagination({	    	
	        totalPages: ${pageCount},
	        visiblePages: ${pageCount}>10?10:${pageCount},
			startPage:${page},
	        version: '1.1',
	        first: "首页",
	        last: "未页",
	        prev: '上一页',
	        next: '下一页',
	        onPageClick: function (event, page) {	        	
	        	if(page!=${page}){
	        	
	        		var params="${params}";
	        		if(params!='choose'){
		        		location.href="<%=request.getContextPath()%>/brandShow/BrandShow_findBrandsByCategory.action?t1.name=${t1.name}&categoryId=${category.id}&page="+ page +"&rows=10";
	        		}
	        		
		        	if(params=='choose'){
					    document.getElementById("defaultForm").action="<%=request.getContextPath()%>/brandShow/BrandShow_findBrandsByCategory.action?params=choose&page="+ page +"&rows=10";
			        	document.getElementById("defaultForm").submit();
	        		}
	        	}	    
	        	
	        }
	    });
    });
    
	//显示添加分类映射的模态框
    function showAddModal(){
    	$('#myModal').modal({keyboard: true});
    }
	
    function choose(entity){
	  	var flag =1;
	  	var pojo=entity;
	  	var page;
	  	if(pojo=='brandShow'){
	  	    page = 	"<%=request.getContextPath()%>/brandShow/BrandShow_listByPage.action?params=choose";
        }
	  	
	  	if(flag == 1){
	  		var titleText = "";
	  		PUBLICOBJECT.id = "entid";
	  		PUBLICOBJECT.title = titleText;
	  		PUBLICOBJECT.width = "500";
	  		PUBLICOBJECT.height = "auto";
	  		PUBLICOBJECT.titleText = titleText;
	  		PUBLICOBJECT.page = page;
	  		var rs = PUBLICOBJECT.openLab();
	  		rs.ShowDialog();// 打开弹出窗
	  		var dom = window.frames["lhgfrm_" + PUBLICOBJECT.id];// 弹出框document对象
	  		rs.addBtn(PUBLICOBJECT.id, "确定", function() {
	  			var _choose = $(dom.document).find("input[name='idChoose']:checked");
	  			var chooseValue = _choose.val();
	  			if(chooseValue != null){
	  				rs.cancel();// 关闭当弹出窗
	  				var chooseName = _choose.siblings("input[name='nameChoose']").val();
	  				if(pojo=='brandShow'){
	  					//$("#brandShowName").trigger("onchange");
	  			    	$("#brandShowId").val(chooseValue);
	  			    	$("#brandShowName").val(chooseName);
	  		        }
	  			}else{
	  				alert("请选择一项");
	  			}
	  		}, "left");
	  	}
    }
    
    //删除
    function deleteMapping(id){
    	if(confirm("确定要删除吗？")){
    		location.href="<%=request.getContextPath()%>/category/category_deleteCategoryBrand?brandId="+id+"&categoryId="+${category.id};
    	}
    }
    
    
    function checkRepeat(){
    	var categoryId = $("#categoryId").val();
    	var brandShowId = $("#brandShowId").val();
    	
    	$.ajax({
     		data:{
				"categoryId":categoryId,
				"brandShowId":brandShowId
			},
     		url:"<%=request.getContextPath()%>/category/category_checkRepeat.action",
     		dataType:"json",
     		success:function(data){
     	    	if(data.success){
     				$("#modalForm").submit();
     			}else{
     				alert(data.message)
     			}  
     		}
     	});
    }
    </script>
</head>

<body>

<c:if test="${params!='choose' }">
<div class="panel panel-primary">
  <div class="panel-heading">分类品牌关联</div>
    <div class="panel-body"> 

		<form class="form-horizontal" id="defaultForm" action="<%=request.getContextPath()%>/brandShow/BrandShow_findBrandsByCategory.action" method="post">
		  	<table>
		  		<tr>
		  		
		  			<td>
		  			<input type="hidden" name="categoryId" value="${category.id}">
		  			<input type="text" name="t1.name" class="form-control" id="name" placeholder="请输入品牌名" style="width: 150px"  value="${t1.name}"></td>
		  			<td><button type="submit" class="btn btn-default">查询</button></td>
		  			<td><button type="button" onclick="showAddModal()" class="btn btn-default">增加</button></td>
		  		</tr>
		  	</table>
		</form>
 
    </div>

  <div class="panel-body"></div>

		<table class="table table-bordered table-hover">
				<tr>
					 <th>品牌名称</th>
					 <th>操作</th>
			        
			    </tr>
		    <s:iterator value="#list">
			    <tr>
					 <td>${name }</td>
					 <td>
					      <%--<a class="btn btn-primary btn-sm" onclick="showUpdateModal('${id }','','')" role="button" >修改</a> --%>
					     <a class="btn btn-primary btn-sm" onclick="deleteMapping('${brandId }')" role="button" >删除</a>
					 </td>
			        
			    </tr>        
		    </s:iterator>
		</table>
</div>

<!-- 模态框（Modal） -->
	<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	    <div class="modal-dialog">
	        <div class="modal-content">
	            <div class="modal-header">
	                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
	                <h4 class="modal-title" id="myModalLabel">4级类别和品牌关联</h4>
	            </div>
	            <div class="modal-body" style="text-align: center">
	            	<form id="modalForm" class="form-horizontal" action="<%=request.getContextPath()%>/category/category_saveCategoryBrand?categoryId=${category.id}" method="post">
	  
						 <div class="form-group">
						    <label for="" class="col-sm-offset-2 col-sm-3 control-label">4级类别名称：</label>
						    <div class="col-sm-3">
						        <input class="form-control" type="text" name="t.name" readonly="readonly" value="${category.name}">
						        <input type="hidden" name="t.id" id="categoryId" value="${category.id}">
						    </div>
						  </div>

		            	<div class="form-group">
						    <label for="" class="col-sm-offset-2 col-sm-3 control-label" id="brandShowNameMng">选择品牌：</label>
						     <div class="col-sm-3">
				                <input class="form-control" type="text" name="brandShowName" id="brandShowName" readonly="readonly" value="${t.brandShow.name }" onchange="checkName('brandShowName')">
				                <input type="hidden" name="brandShowId" id="brandShowId" value="${t.brandShow.id}">
				                <div class="check" style="color:#a94442;font-size:85%"><span class="checkMsg"></span></div>
						     </div>
						     <div class="col-sm-3">
				                <input class="form-control" type="button" value="选择品牌" onclick="choose('brandShow')">
						     </div>
					    </div>
					    
		            	<button type="button" class="btn btn-primary" onclick="checkRepeat()">保存</button>
	            	</form>
	            </div>
	            <input type="hidden" id="nodeId">
	        </div><!-- /.modal-content -->
	    </div><!-- /.modal-dialog -->
	</div>
<!-- /.modal -->
</c:if>



<c:if test="${params=='choose' }">
<div class="panel panel-primary">
  <div class="panel-heading">类别品牌 </div>
    <div class="panel-body"> 

		<form class="form-horizontal" id="defaultForm" action="BrandShow_findBrandsByCategory.action?params=choose" method="post">
		  	<table>
		  		<tr>
		  			<td><input type="text" name="t1.name" class="form-control" id="name" placeholder="请输入品牌名称" style="width: 150px"  value="${t1.name}"></td>
		  				<input type="hidden" name="categoryId" value="${category.id }">
		  			<td><button type="submit" class="btn btn-default">查询</button></td>
		  		</tr>
		  	</table>
		</form>
 
    </div>
  
  
    <table class="table table-bordered table-hover">
        <thead>
	        <tr>
			    <th>品牌名称</th>
	            <th>操作</th>
	        </tr>
        </thead>
        <s:iterator value="#list">
	        <tr>
			    <th>${name}</th>
	            <th>
		            <input type="hidden" name="nameChoose" id="${brandId  }" value="${name  }"/>
				    <input type="radio" name="idChoose" value="${brandId }"/>
	            </th>
	        </tr>        
        </s:iterator>
    </table>  
</div>
</c:if>		


<section id="main-content">
    <p>
        <div class="text-center" display="inline">
            <ul id="pagination-demo" class="pagination-sm">&nbsp;&nbsp;&nbsp;共${pageCount}页,共${recordsCount}条记录</ul>
        </div>
    </p>
</section>


		
</body>
</html>
